package com.diandong.airfieldmanage.main.home.manageMemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.diandong.airfieldmanage.R;
import com.diandong.airfieldmanage.main.home.manageMemo.bean.ChildEntity;
import com.diandong.airfieldmanage.utils.LogUtil;

import java.util.ArrayList;

import androidx.core.content.ContextCompat;

/**
 * Created : Yx
 * Time : 2019/9/5 17:57
 * Describe :
 */
public class ChildAdapter extends BaseExpandableListAdapter {

    private Context mContext;// 上下文
    private int position = 0;//mChilds所属的 1级项 数据下标
    private int mChildsPosition = 0;//该mChilds的下标
    private ArrayList<ChildEntity> mChilds;// 数据源,该集合只有一项数据  Size == 1
    private ChildOnClicks childOnClicks;

    /**
     * @param context       上下文
     * @param position      顶级下标 mChilds所属的 1级项 数据下标
     * @param childPosition 该mChilds的下标
     * @param childs        数据源,该集合只有一项数据  Size == 1
     */
    public ChildAdapter(Context context, int position, int childPosition, ArrayList<ChildEntity> childs) {
        this.mContext = context;
        this.position = position;
        this.mChildsPosition = childPosition;
        this.mChilds = childs;
    }

    public interface ChildOnClicks {
        void onParentSelect(int position, boolean isRefresh);

        void onChildGroupClicks(int position, int mChildsPosition, boolean select);

        void onChildChildClicks(int position, int mChildsPosition, int childPosition, boolean select);
    }

    public void setChildOnClicks(ChildOnClicks clicks) {
        this.childOnClicks = clicks;
    }


    @Override
    public Object getGroup(int groupPosition) {
        if (mChilds != null && mChilds.size() > 0)
            return mChilds.get(groupPosition);
        return null;
    }

    @Override
    public int getGroupCount() {
        return mChilds != null ? mChilds.size() : 0;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mChilds.get(groupPosition).getChildNames() != null ? mChilds.get(groupPosition).getChildNames().size() : 0;
    }

    @Override
    public String getChild(int groupPosition, int childPosition) {
        if (mChilds.get(groupPosition).getChildNames() != null && mChilds.get(groupPosition).getChildNames().size() > 0)
            return mChilds.get(groupPosition).getChildNames().get(childPosition).getName().toString();
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //二级父View
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_add_meeting_seluser_two, null);
            holder = new GroupHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.update(mChilds.get(groupPosition));
        LogUtil.iYx("--标题:--" + mChilds.get(groupPosition).getGroupName());
        boolean isSelect = mChilds.get(groupPosition).getOnSelect();
        if (isSelect) {
            holder.childGroupIV.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.hook_s));
        } else {
            holder.childGroupIV.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.hook_n));
        }
        holder.childGroupIV.setOnClickListener(v -> {
            if (isSelect) {
                mChilds.get(groupPosition).setOnSelect(false);//改变当前数据源状态
                childOnClicks.onChildGroupClicks(position, mChildsPosition, false);//改变最初数据源状态
                for (int i = 0; i < mChilds.get(groupPosition).getChildNames().size(); i++) {
                    mChilds.get(groupPosition).getChildNames().get(i).setOnSelect(false);//改变当前数据源状态
                    childOnClicks.onChildChildClicks(position, mChildsPosition, i, false);//改变最初数据源状态
                }
            } else {
                mChilds.get(groupPosition).setOnSelect(true);//改变当前数据源状态
                childOnClicks.onChildGroupClicks(position, mChildsPosition, true);//改变最初数据源状态
                for (int i = 0; i < mChilds.get(groupPosition).getChildNames().size(); i++) {
                    mChilds.get(groupPosition).getChildNames().get(i).setOnSelect(true);//改变当前数据源状态
                    childOnClicks.onChildChildClicks(position, mChildsPosition, i, true);//改变最初数据源状态
                }
            }
            childOnClicks.onParentSelect(position, true);//检查该一级position中所有二级下标是否全选

            notifyDataSetChanged();
        });

        return convertView;
    }

    //对应的三级子View
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ChildHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_add_meeting_seluser_three, null);
            holder = new ChildHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        holder.update(getChild(groupPosition, childPosition));
        boolean isSelect = mChilds.get(groupPosition).getChildNames().get(childPosition).getOnSelect();
        if (isSelect) {
            holder.childChildIV.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.hook_s));
        } else {
            holder.childChildIV.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.hook_n));
        }

        holder.childChildIV.setOnClickListener(v -> {
            if (isSelect) {
                mChilds.get(groupPosition).getChildNames().get(childPosition).setOnSelect(false);//改变当前数据源状态
                childOnClicks.onChildChildClicks(position, mChildsPosition, childPosition, false);//改变最初数据源状态
            } else {
                mChilds.get(groupPosition).getChildNames().get(childPosition).setOnSelect(true);//改变当前数据源状态
                childOnClicks.onChildChildClicks(position, mChildsPosition, childPosition, true);//改变最初数据源状态
            }
            boolean allSelect = true;//该三级列表是否全选
            for (int i = 0; i < mChilds.get(groupPosition).getChildNames().size(); i++) {
                if (!mChilds.get(groupPosition).getChildNames().get(i).getOnSelect()) {
                    allSelect = false;
                    break;
                }
            }
            if (allSelect) {
                mChilds.get(groupPosition).setOnSelect(true);//改变当前数据源状态
                childOnClicks.onChildGroupClicks(position, mChildsPosition, true);//改变最初数据源状态
            } else {
                mChilds.get(groupPosition).setOnSelect(false);//改变当前数据源状态
                childOnClicks.onChildGroupClicks(position, mChildsPosition, false);//改变最初数据源状态
            }
            childOnClicks.onParentSelect(position, false);//检查该一级position中所有二级下标是否全选
            notifyDataSetChanged();

        });

        return convertView;
    }


    /**
     * Holder优化 - 父
     */
    class GroupHolder {

        private TextView childGroupTV;
        private ImageView childGroupIV;

        public GroupHolder(View v) {
            childGroupTV = (TextView) v.findViewById(R.id.amsu_title);
            childGroupIV = (ImageView) v.findViewById(R.id.amsu_select);
        }

        public void update(ChildEntity model) {
            childGroupTV.setText(model.getGroupName());
        }
    }

    /**
     * Holder优化  - 子
     */
    class ChildHolder {

        private TextView childChildTV;
        private ImageView childChildIV;

        public ChildHolder(View v) {
            childChildTV = (TextView) v.findViewById(R.id.amsu_title);
            childChildIV = (ImageView) v.findViewById(R.id.amsu_select);
        }

        public void update(String str) {
            childChildTV.setText(str);
        }
    }


    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        /**
         * ==============================================
         * 此处必须返回true，否则无法响应子项的点击事件===============
         * ==============================================
         **/
        return true;
    }

}
